// pages/order/order.js
const app = getApp();
const util = require("../../utils/util.js");
const api = require("../../utils/api.js");
Page({
    data: {
        pageColor: app.pageColor.mainColor,
        listItem: ["全部", "待付款", "待发货", "待收货"],
        currentTab: 0, //预设当前项的值
        id: "0",
        orderInfo: {},
        order_sn:""
    },

    /**
     * 进入订单详情
     */
    toOrderInfo:function(e){
        var ordersn = e.currentTarget.dataset.ordersn
        wx.navigateTo({
            url: '../orderInfo/orderInfo?ordersn=' + ordersn
        })
    },

    // 切换订单状态
    onTapOrderState: function (e) {
        var pos = typeof e == "string" ? e : e.currentTarget.dataset.pos;
        this.setData({
            currentTab: pos
        })
        //请求订单信息
        this.getOrderInfo(pos);
    },

    /**
     * 获取订单信息
     */
    getOrderInfo(state) {
        let url = api.myOrder();
        util.http(true,"GET", url, { pay_status: state }, this.getOrderInfoCallBack)
    },

    /**
     * 获取订单信息回调
     */
    getOrderInfoCallBack: function (res) {
        this.setData({
            orderInfo: res
        })
    },

    onLoad: function (e) {
        //获取从我的界面传来的下标
        var id = e.id == undefined ? "0" : e.id
        this.setData({
            id: id,
        })
    },

    onShow: function () {
        //切换订单状态
        this.onTapOrderState(this.data.id);
    },

    onReady: function () {
        util.setNavigationBarTitle("订单");
    },

    /**
     * 取消订单
     */
    orderCancel: function (e) {
        var that = this;
        let url = api.cancelOrder();
        util.showModal("", "确认取消订单吗?", function () {
            //确认发起取消请求
            var order_sn = e.currentTarget.dataset.ordersn
            util.http(true, "POST", url, { order_sn: order_sn }, function (res) {
                //请求订单信息
                that.getOrderInfo(that.data.currentTab);
            })
        })
    },
    /**
     * 删除订单
     */
    removeOrder: function (e) {
        var that = this;
        let url = api.delOrder();
        util.showModal("", "确认删除订单吗?", function () {
            //确认发起删除请求
            var order_sn = e.currentTarget.dataset.ordersn
            util.http(true, "POST", url, { order_sn: order_sn },function (res) {
                //重新请求订单信息
                that.getOrderInfo(that.data.currentTab);
            })
        })
    },
    /**
     * 立即支付
     */
    pay: function (e) {
        var order_sn = e.currentTarget.dataset.ordersn
        this.setData({
            order_sn: order_sn
        })
        //获取openID
        util.getOpenID(app.globalData.appid, app.globalData.secret, this.getOpenIDCallBack);
    },

    /**
     * 获取openID回调
     */
    getOpenIDCallBack:function(openid) {
        console.log("getOpenIDCallBack========"+openid);
        this.getPayInfo(openid);
    },
    /**
     * 获取支付信息
     */
    getPayInfo: function (openid) {
        let url = api.orderPay();
        util.http(true,"POST", url, { app_id: app.globalData.appid, order_sn: this.data.order_sn, openid: openid }, this.getPayInfoCallBack)
    },

    /**
     * 支付信息回调
     */
    getPayInfoCallBack: function (res) {
        //发起支付
        this.requestPay(res);
    },
    /**
     * 发起支付
     */
    requestPay: function (res) {
        util.pay(res, this.requestPayCallBack)
    },
    /**
     * 吊起支付回调
     */
    requestPayCallBack: function (res) {
        this.navigateTo("../order/order")
    },

    /**
     * 提醒发货
     */
    remind: function (e) {
        var that = this;
        let url = api.remind();
        util.showModal("", "确认提醒商家吗?", function () {
            //确认发起删除请求
            console.log(e)
            var order_sn = e.currentTarget.dataset.ordersn;
            var remind = e.currentTarget.dataset.remind;
            if(remind ==2){
                util.successToast("提醒成功！")
                return;
            }
            util.http(false,"POST", url, { order_sn: order_sn }, function (res) {
                //重新请求订单信息
                that.getOrderInfo(that.data.currentTab);
            })
        })
    },
    /**
     * 参看物流
     */
    logistics: function (e) {
        console.log(e);
        wx.navigateTo({
            url:"../logDetail/logDetail?ordersn=" + e.currentTarget.dataset.ordersn
        });
    },

    /**
     * 确认收获
     */
    takeGoods: function (e) {
        var that = this;
        let url = api.confirm();
        util.showModal("", "确认收获吗?", function () {
            //确认发起删除请求
            var order_sn = e.currentTarget.dataset.ordersn
            util.http("POST", url, { order_sn: order_sn }, function (res) {
                //重新请求订单信息
                that.getOrderInfo(that.data.currentTab);
            })
        })
    },
    /**
     * 回到首页
     */
    goIndex: function () {
        wx.switchTab({
            url: '../index/index'
        })
    },
})